package cc.sheji.controller.admin;

import cc.sheji.Service.NoticeService;
import cc.sheji.domain.AdQueryCriteria;
import cc.sheji.domain.JsonResult;
import cc.sheji.domain.Notice;
import cc.sheji.domain.NoticeQueryCriteria;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController
@RequestMapping(value = "/admin/v1/notice")
public class AdminNoticeController {

    @Autowired
    NoticeService noticeService;
    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(noticeService.get(id));
    }

    @PostMapping("")
    public JsonResult create(@RequestBody @Validated() Notice entity) {
        entity.setId(null);
        return JsonResult.Success(noticeService.create(entity));
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @Valid Notice entity) {
        entity.setId(id);
        return JsonResult.Success(noticeService.update(entity));
    }

    @GetMapping("/mine")
    public JsonResult mine(NoticeQueryCriteria criteria , Pageable pageable) throws InterruptedException {
        criteria.setUserId(StpUtil.getLoginIdAsLong());
        return JsonResult.Success(noticeService.list(criteria,pageable));
    }
    @GetMapping("")
    public JsonResult get(NoticeQueryCriteria criteria , Pageable pageable) throws InterruptedException {

        return JsonResult.Success(noticeService.list(criteria,pageable));
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable(name = "id") Long id) {
        return JsonResult.Success(noticeService.delete(id));
    }


}
